/**
 * 
 */
package cn.bblocks.cacheTest.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.redis.support.Page4Navigator;
//import org.springframework.data.redis.support.Page4Navigator;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;



import cn.bblocks.cacheTest.model.Area;
import cn.bblocks.cacheTest.model.User;
import cn.bblocks.cacheTest.model.UserBank;
import cn.bblocks.cacheTest.service.AreaService;
import cn.bblocks.cacheTest.service.UserBankService;
import cn.bblocks.cacheTest.service.UserService;
import cn.bblocks.cacheTest.suport.FastJsonUtils;
import cn.bblocks.cacheTest.suport.RandomValue;
import cn.bblocks.cacheTest.suport.dto.response.Response;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * @author jin
 *
 */
@Slf4j
@RestController
public class IndexController {
	
	@Autowired
	private AreaService areaService;
	@Autowired
	private UserBankService userBankService;
	@Autowired
	private UserService userService;
	
	@ApiOperation(value = "hello")
	@RequestMapping(value = "/index", method = RequestMethod.GET)
	public String index() {
		my = userBankService.getMyBanks(1);
		//myStr = JsonUtil.obj2String(my);
		myStr = FastJsonUtils.convertObjectToJSON(my);
		return "hello wenker!";
	}
	
//	@ApiOperation(value = "hello")
//	@RequestMapping(value = "/index", method = RequestMethod.GET)
//	public String save() {
//		return areaService.insertSelective(area);
//	}
	List<UserBank> my = null;
	String myStr = null;
	
	
	@ApiOperation(value = "更新")
	@ApiImplicitParams({
        @ApiImplicitParam(name = "areaId", value = "id", required = true,paramType = "query" ),
     })
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public Response<Area> update(Integer areaId) {
		Area area = areaService.selectByPrimaryKey(areaId);
		area.setUpdateTime(new Date());
		
		return Response.getInstance(areaService.updateByPrimaryKey(area,"parentid","areacode")) ;
	}
	
	@ApiOperation(value = "获取")
	@ApiImplicitParams({
        @ApiImplicitParam(name = "areaId", value = "id", required = true,paramType = "query" ),
     })
	@RequestMapping(value = "/get", method = RequestMethod.GET)
	public Response<Area> get(Integer areaId) {
		Area area = areaService.selectByPrimaryKey(areaId);
		return Response.getInstance(  area);
	}
	
	@ApiOperation(value = "获取")
	@ApiImplicitParams({
        @ApiImplicitParam(name = "code", value = "code", required = true,paramType = "query" ),
     })
	@RequestMapping(value = "/getByCode", method = RequestMethod.GET)
	public Response<Area> selectByCode(String code) {
		Area area = areaService.selectByCode(code);
		return Response.getInstance(  area);
	}
	
	
	
	@ApiOperation(value = "获取集合")
	@ApiImplicitParams({
        @ApiImplicitParam(name = "areaId", value = "id", required = true,paramType = "query" ),
     })
	@RequestMapping(value = "/getChildren", method = RequestMethod.GET)
	public Response<List<Area>> getChildren(Integer areaId) {
		
		return Response.getInstance( areaService.getChildren(areaId) );
	}
	
	
	@ApiOperation(value = "随便新增一个（模拟新增 和 删除）")
	@ApiImplicitParams({
        
     })
	@RequestMapping(value = "/addOne", method = RequestMethod.PUT)
	public Response<List<Area>> addOne(Integer pid) {
		Area area = new Area();
		area.setAreacode("code"+System.currentTimeMillis());
		area.setAreaname("name"+System.currentTimeMillis());
		area.setLevel(100);
		area.setParentid(pid);
	
		return Response.getInstance( areaService.insertSelective(area) );
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	@ApiOperation(value = "用户银行卡")
	@ApiImplicitParams({
        @ApiImplicitParam(name = "loginName", value = "loginName", required = true,paramType = "query",defaultValue="15201080314"  ),
     })
	@RequestMapping(value = "/userBanks", method = RequestMethod.GET)
	public Response<List<UserBank>> userBanks(String loginName) {
		User user = userService.selectByLoginName(loginName);
		
		if(user != null) {
			//System.out.println(b);
			//log.info("loginName:{}",loginName);
			List<UserBank> list = userBankService.getMyBanks(user.getId());
			return Response.getInstance( list );
		}else {
			
			return Response.nonExpect(1);
		}
		//return Response.getInstance( userBankService.getMyBanks(1) );
		
//		return Response.getInstance( my );
	} 
	
	@ApiOperation(value = "用户Page银行卡")
	@ApiImplicitParams({
        @ApiImplicitParam(name = "loginName", value = "loginName", required = true,paramType = "query",defaultValue="15201080314"  ),
     })
	@RequestMapping(value = "/userPageBanks", method = RequestMethod.GET)
	public Response<Page4Navigator<UserBank>> userPageBanks(String loginName
			,@PageableDefault(sort = { "id" }, direction = Sort.Direction.DESC) Pageable pageable) {
		User user = userService.selectByLoginName(loginName);
		
		if(user != null) {
			return Response.getInstance( userBankService.getMyPageBanks(user.getId(),pageable) );
		}else {
			
			return Response.nonExpect(1);
		}
		//return Response.getInstance( userBankService.getMyBanks(1) );
		
//		return Response.getInstance( my );
	} 
	
	
	@ApiOperation(value = "用户银行卡")
	@ApiImplicitParams({
        @ApiImplicitParam(name = "loginName", value = "loginName", required = true,paramType = "query" ,defaultValue="15201080314" ),
     })
	@RequestMapping(value = "/userBanks2", method = RequestMethod.GET)
	public void userBanks2(String loginName,HttpServletResponse resp ) {
		//User user = userService.selectByLoginName(loginName);
		;
		//return Response.getInstance( userBankService.getMyBanks(user.getId()) );
//		return Response.getInstance( userBankService.getMyBanks(1) );
		//写到客户端
	
		PrintWriter out = null;
	    try {
	        //设定类容为json的格式
	        resp.setContentType("application/json;charset=UTF-8");
	        out = resp.getWriter();
	        //写到客户端
	        out.write(myStr);
	        out.flush();
	    } catch (IOException e) {
	        e.printStackTrace();
	    }finally{
	        if(out != null){
	            out.close();
	        }
	    }

	        
	 
	} 
	
	@ApiOperation(value = "用户银行卡详情")
	@ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "id", required = true,paramType = "query" ),
     })
	@RequestMapping(value = "/userBankInfo", method = RequestMethod.GET)
	public Response<UserBank> userBankInfo(Integer id) {
		
		return Response.getInstance( userBankService.getBank(id) );
	} 
	
	
	
	
	
	
	@ApiOperation(value = "用户银行卡修改(核心)")
	@ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "id", required = true,paramType = "query" ),
     })
	@RequestMapping(value = "/userBankInfoUpdate2", method = RequestMethod.PUT)
	public Response<UserBank> userBankInfoUpdate2(Integer id) {
		UserBank bank = userBankService.getBank(id);
		bank.setModifyTime(new Date());

		return Response.getInstance( userBankService.update2(bank) );
	} 
	
	
	
	
	@ApiOperation(value = "用户随机+")
	@RequestMapping(value = "/userAdd", method = RequestMethod.POST)
	public Response userAdd() {
		User user = new User();
		user.setLoginName( RandomValue.getTel() );
		user.setEmail(RandomValue.getEmail(10, 18));
		user.setName(RandomValue.getChineseName());
		user.setStatus(1);
		
		userService.addUser(user);
		
		return Response.getInstance( "OK" );
	} 
	
	@ApiOperation(value = "用户卡随机+")
	@ApiImplicitParams({
        @ApiImplicitParam(name = "loginName", value = "loginName", required = true,paramType = "query" ),
     })
	@RequestMapping(value = "/userBankAdd", method = RequestMethod.POST)
	public Response userBankAdd(String loginName) {
		
		User user = userService.selectByLoginName(loginName);

		UserBank onebank = new UserBank();
		onebank.setIdcardseqno(user.getId());
		onebank.setCreateTime(new Date());
	
		onebank.setMobile(RandomValue.getTel());
		//62266609 0168 3439
		onebank.setNum("62266609"+RandomValue.getNum(1000, 10000) + RandomValue.getNum(1000, 10000));
		onebank.setRealName(user.getName());
		onebank.setType("xx银行");
		onebank.setStatus(1);
		
		userBankService.add(onebank);
		

		return Response.getInstance( "OK" );
	} 
	
	@ApiOperation(value = "用户银行卡修改(非核心)")
	@ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "id", required = true,paramType = "query" ),
     })
	@RequestMapping(value = "/userBankInfoUpdate", method = RequestMethod.PUT)
	public Response<UserBank> userBankInfoUpdate(Integer id) {
		UserBank bank = userBankService.getBank(id);
		bank.setModifyTime(new Date());

		return Response.getInstance( userBankService.update(bank) );
	} 
	
	@ApiOperation(value = "用户银行卡删除（类似核心修改）")
	@ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "id", required = true,paramType = "query" ),
     })
	@RequestMapping(value = "/userBankInfoDel", method = RequestMethod.DELETE)
	public Response userBankInfoDel(Integer id) {
		UserBank bank = userBankService.getBank(id);
		if(bank != null)
			userBankService.deleteByPrimaryKey(bank);
		
		return Response.getInstance( "" );
	} 
	
	
	@ApiOperation(value = "测试日志")
	@RequestMapping(value = "/log", method = RequestMethod.GET)
	public Response log() {
		
		log.info("测试日志");
		
		return Response.ok();
	} 

}
